package com.bening.smsapp.downloader;

import org.apache.log4j.Logger;
import com.bening.smsapp.bootstrap.BootConfigParameters;
import com.bening.smsapp.exception.SmsAppException;
import java.util.*;
import com.bening.smsapp.bean.*;
import com.bening.smsapp.constant.SmsAppConstant;

public class FtpDownloaderProcess
{
    /**
     *
     * @param params
     */
    public static void process( BootConfigParameters params )
    {
        final Logger logger = Logger.getLogger( FtpDownloaderProcess.class );
        String METHOD_SIGNATURE = "[ftp]";
        System.out.println("[Downloader]");
        
        FtpClient ftpClient = new FtpClient();
        List machine = params.getMachineSourceList();
        
        for(int i=0;i<machine.size();i++) {
        	MachineSource m = (MachineSource)machine.get(i);
        	if(m.getMachineType() == SmsAppConstant.FLAG_REMOTE_MACHINE) { 
		        	if(m.getSourcePathList().size()!=0) {
				        String host     = m.getIpAddress();
				        int port        = m.getPort();
				        String username = m.getUsername();
				        String password = m.getPassword();
				        
				        try
				        {
				        	
				        	logger.info( METHOD_SIGNATURE + "logging in...." );
				            ftpClient.login( host, port, username, password );
				            List sourceList = m.getSourcePathList();
				        	
				            for(int j=0;j<sourceList.size();j++)  {
				        		PathSource p = (PathSource) sourceList.get(j);
				        		ftpClient.downloadRegisteredList( p, m, params );
				        		
				        	}
				            ftpClient.logout();
				        }
				        catch ( SmsAppException e )
				        {
				            logger.error( e.getMessage(), e );
				            
				        }
		        	}
        	}
	      }
    }

}
